home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / ftplugin / man.vim < prev    next >
Encoding:
Text File  |  2001-09-20  |  4.1 KB  |  176 lines

  1. " Vim filetype plugin file
  2. " Language:    man
  3. " Maintainer:    Nam SungHyun <namsh@kldp.org>
  4. " Last Change:    2001 Sep 20
  5.  
  6. " To make the ":Man" command available before editing a manual page, source
  7. " this script from your startup vimrc file.
  8.  
  9. " If 'filetype' isn't "man", we must have been called to only define ":Man".
  10. if &filetype == "man"
  11.  
  12.   " Only do this when not done yet for this buffer
  13.   if exists("b:did_ftplugin")
  14.     finish
  15.   endif
  16.   let b:did_ftplugin = 1
  17.  
  18.   " allow dot in manual page name.
  19.   setlocal iskeyword+=\.
  20.  
  21.   " Add mappings, unless the user didn't want this.
  22.   if !exists("no_plugin_maps") && !exists("no_man_maps")
  23.     if !hasmapto('<Plug>ManBS')
  24.       nmap <buffer> <LocalLeader>h <Plug>ManBS
  25.     endif
  26.     nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:set nomod<CR>''
  27.  
  28.     nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
  29.     nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
  30.   endif
  31.  
  32. endif
  33.  
  34. if exists(":Man") != 2
  35.   com -nargs=1 Man call s:GetPage(<f-args>)
  36.   nmap <Leader>K :call <SID>PreGetPage(0)<CR>
  37. endif
  38.  
  39. " Define functions only once.
  40. if !exists("s:man_tag_depth")
  41.  
  42. let s:man_tag_depth = 0
  43.  
  44. if $OSTYPE =~ "solaris"
  45.   let s:man_sect_arg = "-s"
  46.   let s:man_find_arg = "-l"
  47. else
  48.   let s:man_sect_arg = ""
  49.   let s:man_find_arg = "-w"
  50. endif
  51.  
  52. func <SID>PreGetPage(cnt)
  53.   if a:cnt == 0
  54.     let old_isk = &iskeyword
  55.     setl iskeyword+=(,)
  56.     let str = expand("<cword>")
  57.     let &iskeyword = old_isk
  58.     let page = substitute(str, '(*\(\k\+\).*', '\1', '')
  59.     let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
  60.     if match(sect, '^[0-9 ]\+$') == -1
  61.       let sect = ""
  62.     endif
  63.     if sect == page
  64.       let sect = ""
  65.     endif
  66.   else
  67.     let sect = a:cnt
  68.     let page = expand("<cword>")
  69.   endif
  70.   call s:GetPage(sect, page)
  71. endfunc
  72.  
  73. func <SID>GetCmdArg(sect, page)
  74.   if a:sect == ''
  75.     return a:page
  76.   endif
  77.   return s:man_sect_arg.' '.a:sect.' '.a:page
  78. endfunc
  79.  
  80. func <SID>FindPage(sect, page)
  81.   let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
  82.   if where !~ "^/"
  83.     if substitute(where, ".* \\(.*$\\)", "\\1", "") !~ "^/"
  84.       return 0
  85.     endif
  86.   endif
  87.   return 1
  88. endfunc
  89.  
  90. func <SID>GetPage(...)
  91.   if a:0 >= 2
  92.     let sect = a:1
  93.     let page = a:2
  94.   elseif a:0 >= 1
  95.     let sect = ""
  96.     let page = a:1
  97.   else
  98.     return
  99.   endif
  100.  
  101.   " To support:        nmap K :Man <cword>
  102.   if page == '<cword>'
  103.     let page = expand('<cword>')
  104.   endif
  105.  
  106.   if sect != "" && s:FindPage(sect, page) == 0
  107.     let sect = ""
  108.   endif
  109.   if s:FindPage(sect, page) == 0
  110.     echo "\nCannot find a '".page."'."
  111.     return
  112.   endif
  113.   exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
  114.   exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
  115.   exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
  116.   let s:man_tag_depth = s:man_tag_depth + 1
  117.  
  118.   " Use an existing "man" window if it exists, otherwise open a new one.
  119.   if &filetype != "man"
  120.     let thiswin = winnr()
  121.     exe "norm! \<C-W>b"
  122.     if winnr() == 1
  123.       new
  124.     else
  125.       exe "norm! " . thiswin . "\<C-W>w"
  126.       while 1
  127.     if &filetype == "man"
  128.       break
  129.     endif
  130.     exe "norm! \<C-W>w"
  131.     if thiswin == winnr()
  132.       new
  133.       break
  134.     endif
  135.       endwhile
  136.     endif
  137.   endif
  138.   silent exec "edit $HOME/".page.".".sect."~"
  139.  
  140.   set ma
  141.   silent exec "norm 1GdG"
  142.   let $MANWIDTH = winwidth(0)
  143.   silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
  144.   " Is it OK?  It's for remove blank or message line.
  145.   if getline(1) =~ "^\s*$"
  146.     silent exec "norm 2G/^[^\s]\<cr>kd1G"
  147.   endif
  148.   if getline('$') == ''
  149.     silent exec "norm G?^\s*[^\s]\<cr>2jdG"
  150.   endif
  151.   1
  152.   setl ft=man nomod
  153.   setl bufhidden=hide
  154.   setl nobuflisted
  155. endfunc
  156.  
  157. func <SID>PopPage()
  158.   if s:man_tag_depth > 0
  159.     let s:man_tag_depth = s:man_tag_depth - 1
  160.     exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
  161.     exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
  162.     exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
  163.     exec s:man_tag_buf."b"
  164.     exec s:man_tag_lin
  165.     exec "norm ".s:man_tag_col."|"
  166.     exec "unlet s:man_tag_buf_".s:man_tag_depth
  167.     exec "unlet s:man_tag_lin_".s:man_tag_depth
  168.     exec "unlet s:man_tag_col_".s:man_tag_depth
  169.     unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
  170.   endif
  171. endfunc
  172.  
  173. endif
  174.  
  175. " vim: set sw=2:
  176.